cc.Class({
    extends: cc.Component,

    properties: {
    },

    onLoad() {
        cc.Sound = this;
        this.isMute = true;
        this.sounds = {};
        this.musicIds = {};
    },

    isSoundOff() {
        return cc.sys.localStorage.getItem('SOUND_STATE') == 'off';
    },

    pauseAll() {
        this.isMute = true;
        cc.audioEngine.pauseAll();
        cc.sys.localStorage.setItem('SOUND_STATE', 'off');
    },

    resumeAll() {
        this.isMute = false;
        cc.audioEngine.resumeAll();
        cc.sys.localStorage.setItem('SOUND_STATE', 'on');
    },

    toggleSound() {
        if (this.isMute) this.resumeAll();
        else this.pauseAll();
    },

    playMusic(music) {
        if (!this.isMute) {
            if (!this.sounds[music]) {
                cc.loadRes('sound/' + music, res => {
                    this.sounds[music] = res;
                    this.musicIds[music] = cc.audioEngine.playMusic(res, true);
                });
            } else {
                this.musicIds[music] = cc.audioEngine.playMusic(this.sounds[music], true);
            }
        }
    },

    pauseMusic(music) {
        this.musicIds[music] && cc.audioEngine.stop(this.musicIds[music]);
    },

    playEffect(snd) {
        if (!this.isMute) {
            if (!this.sounds[snd]) {
                cc.loadRes('sound/' + snd, res => {
                    this.sounds[snd] = res;
                    cc.audioEngine.playEffect(res, false);
                });
            } else {
                cc.audioEngine.playEffect(this.sounds[snd], false);
            }
        }
    }

});
